
"""
This file is part of PeerTool, a web application for collaborative
decision-making (Copyrighted, 2009-2010, Ellis & Grant, Inc.)
Developed by Michael F. Ellis <michael.f.ellis@gmail.com>.  
License: AGPL v3
PeerTool comes with ABSOLUTELY NO WARRANTY.
"""                
from random import choice, random
import cPickle as pickle
import ptproblem as P
## create a problem object
p = P.Problem("This is my problem",1)
print "Description:", p.text
print "Team Members:", p.lastresponses
print p.criteria
def dumpresponses(obj):
    for id,c in obj.iteritems():
        print id, c.text
        for memberid,response in c.responses.iteritems():
            print "\t", memberid, response
        if isinstance(c,P.Criterion):
            for memberid,response in c.normalizedresponses.iteritems():
                print "\t", memberid, response
            
print p.solutions
p.addTeamMember(2)
p.acceptResponses(2,{0:.75},{0:([0,-.25],)})
dumpresponses(p.criteria)
dumpresponses(p.solutions)
p.acceptResponses(1,{0:0.}, {0:([0,.25],)})
dumpresponses(p.criteria)
dumpresponses(p.solutions)
p.computeNormalizedCriteria()
print "After computing normalized criteria responses"
dumpresponses(p.criteria)
dumpresponses(p.solutions)
cresults,sresults = p.computeCurrentEvaluations()
print cresults, sresults
p.addSolution(P.Solution("Another solution"))
print "After adding another solution (without voting)"
cresults,sresults = p.computeCurrentEvaluations()
print cresults, sresults

p.addCriterion(P.Criterion("First Criterion"))
print "After 1 vote ..."
p.acceptResponses(1,{0:.5,1:.75}, 
    {0:([0,.25],[1,-.25]), 1:([0,-.75],[1,.5])})
cresults,sresults = p.computeCurrentEvaluations()
print cresults, sresults

#change a solution's text and get the responses
p.solutions[1].updateText("Another Solution modified")
r = p.getLastResponses(1)
print r.criteriaresponses
print r.changedcriteria
print r.solutionresponses
print r.changedsolutions


## Sanity checks for criteria weighting. Random choices for criteria values with equal votes by all participants on all criteria for solutions 0 and 1

failures = 0
def w():
    return random()

for i in xrange(100):
    for j in range(10):
        v = 2*random() - 1.0
        p.acceptResponses(1,{0:w(),1:w()}, 
        {0:([0,v],[1,v]), 1:([0,-v],[1,-v])}) 
        p.acceptResponses(2,{0:w(),1:w()}, 
        {0:([0,v],[1,v]), 1:([0,-v],[1,-v])}) 
        cresults,sresults = p.computeCurrentEvaluations()
        try:
            assert abs((sresults[0][0] + sresults[0][1])/2 - float(v)) < 1e-15
            assert abs((sresults[1][0] + sresults[1][1])/2 - float(-v)) < 1e-15
        except AssertionError:
            failures += 1
            print v, cresults, sresults, "Failed!"
            r = p.getLastResponses(1)
            print r.criteriaresponses
            print p.criteria[0].normalizedresponses[1]
            r = p.getLastResponses(2)
            print r.criteriaresponses
            print p.criteria[0].normalizedresponses[2]
print "Criteria Weighting sanity checks: %(failures)d failures."%locals()            
print "\nAbout to test pickling a problem object"
f = open("/tmp/foo", 'w')
pickle.dump(p,f)
f.close()
f1 = open('/tmp/foo','r')
q = pickle.load(f1)
f1.close()
cresults,sresults = p.computeCurrentEvaluations()
print "Original: ", cresults, sresults
cresults,sresults = q.computeCurrentEvaluations()
print "Unpickled: ", cresults, sresults

print "About to print solutions sorted by reverse chrono"
sorted = p.sortSolutionsReverseChrono()
print sorted
assert sorted[0][1].lastmodified > sorted[1][1].lastmodified


print "\nDelete a user"
q.delTeamMember(2)
cresults,sresults = q.computeCurrentEvaluations()
print "After member delete: ", cresults, sresults         

print "\nDelete a criterion."
q.delCriterion(0)
cresults,sresults = q.computeCurrentEvaluations()
print "After criterion delete: ", cresults, sresults         

print "\nDelete a solution"
q.delSolution(0)
cresults,sresults = q.computeCurrentEvaluations()
print "After solution delete", cresults, sresults

print "\nSubmit a new response"
q.acceptResponses(1,{1:.75}, 
    {1:([0,-.75],[1,.5])})
cresults,sresults = q.computeCurrentEvaluations()
print "After new response", cresults, sresults

print p.uuid
print q.uuid

print "\nText getLabel() function"
print p.getLabel(10)
print p.getLabel(500)
